//
//  FriendParser.m
//  FootPrint
//
//  Created by alex tao on 4/9/13.
//  Copyright (c) 2013 alex tao. All rights reserved.
//

#import "FriendParser.h"
#import "FriendManager.h"
#import "FriendQueryRequest.pb.h"
#import "GroupQueryRequest.pb.h"
#import "GroupFriendAddRequest.pb.h"
#import "GroupFriendRemoveRequest.pb.h"
#import "GroupCreateRequest.pb.h"
#import "GroupDeleteRequest.pb.h"

@implementation FriendParser

- (void)dealloc
{
    _gDelegate = nil;
    _delegate = nil;
    [super dealloc];
}

- (void) updateFriendListForUid:(long long)uid
{
    if (uid <= 0) {
        return;
    }
    
    FriendQueryRequestMessage_Builder * builder = [[FriendQueryRequestMessage builder] setUserId:uid];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_FRIEND_LIST postData:queryData completion:^(NSData * resultData) {
            @try {
                FriendQueryResponseMessage * resp = [FriendQueryResponseMessage parseFromData:resultData];
                [_delegate didRecieveFriendList:resp];
                [[FriendManager sharedInstance] mergeFriendList:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"获取好友列表失败信息!"] setDuration:2000] show];
                [_delegate didRecieveFriendList:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didRecieveFriendList:nil];
        }];
    }
}

- (void) updateGroupListForUid:(long long)uid
{
    if (uid <= 0) {
        [_delegate didRecieveGroupList:nil];
        return;
    }
    
    GroupQueryRequestMessage_Builder * builder = [[GroupQueryRequestMessage builder] setUserId:uid];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_GROUP_QUERY postData:queryData completion:^(NSData * resultData) {
            @try {
                GroupQueryResponseMessage * resp = [GroupQueryResponseMessage parseFromData:resultData];
                [_delegate didRecieveGroupList:resp];
                [[FriendManager sharedInstance] mergeGroupList:resp];
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"获取好友分组列表失败信息!"] setDuration:2000] show];
                [_delegate didRecieveGroupList:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didRecieveGroupList:nil];
        }];
    }
}

- (void) addGroupMember:(long long)gId withMemberList:(NSArray*)ids
{
    if (gId <= 0 || ids.count == 0) {
        return;
    }
 
    GroupFriendAddRequestMessage_Builder * builder = [[[GroupFriendAddRequestMessage builder] setGroupId:gId] setUserIdListArray:ids];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_GROUP_ADD_MEMBER postData:queryData completion:^(NSData * resultData) {
            @try {
                GroupFriendAddResponseMessage * resp = [GroupFriendAddResponseMessage parseFromData:resultData];
                [_gDelegate didAddGroupFriends:resp];
                [FriendManager sharedInstance].isLatestGroup = NO;
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"添加好友失败!"] setDuration:2000] show];
                [_gDelegate didAddGroupFriends:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_gDelegate didAddGroupFriends:nil];
        }];
    }
}

- (void) removeGroupMember:(long long)gId withMemberList:(NSArray*)ids
{
    if (gId <= 0 || ids.count == 0) {
        [_gDelegate didRemoveGroupFriends:nil];
        return;
    }
    
    GroupFriendRemoveRequestMessage_Builder * builder = [[[GroupFriendRemoveRequestMessage builder] setGroupId:gId] setUserIdListArray:ids];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_GROUP_REMOVE_MEMBER postData:queryData completion:^(NSData * resultData) {
            @try {
                GroupFriendRemoveResponseMessage * resp = [GroupFriendRemoveResponseMessage parseFromData:resultData];
                [_gDelegate didRemoveGroupFriends:resp];
                [FriendManager sharedInstance].isLatestGroup = NO;
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"删除好友失败!"] setDuration:2000] show];
                [_gDelegate didRemoveGroupFriends:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_gDelegate didRemoveGroupFriends:nil];
        }];
    }
}

- (void) createGroup:(long long)uid forName:(NSString*)name withMembers:(NSArray*)userMsgs
{
    if (uid <= 0 || name.length == 0 || userMsgs.count == 0) {
        [_gDelegate didCreateGroup:nil];
        return;
    }
    
    GroupMessage * groupMsg = [[[[[GroupMessage builder] setId:0] setName:name] setUsersArray:userMsgs] build];
    GroupCreateRequestMessage_Builder * builder = [[[GroupCreateRequestMessage builder] setGroup:groupMsg] setUserId:uid];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_GROUP_CREATE postData:queryData completion:^(NSData * resultData) {
            @try {
                GroupCreateResponseMessage * resp = [GroupCreateResponseMessage parseFromData:resultData];
                [_gDelegate didCreateGroup:resp];
                [FriendManager sharedInstance].isLatestGroup = NO;
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"创建组失败!"] setDuration:2000] show];
                [_gDelegate didCreateGroup:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_gDelegate didCreateGroup:nil];
        }];
    }
}

- (void) deleteGroup:(long long)uid forGroupId:(long long)gid
{
    if (uid <= 0 || gid <= 0) {
        [_delegate didDeleteGroup:nil];
        return;
    }
    
    GroupDeleteRequestMessage_Builder * builder = [[[GroupDeleteRequestMessage builder] setUserId:uid] setGroupId:gid];
    
    NSData * queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_GROUP_DEL postData:queryData completion:^(NSData * resultData) {
            @try {
                GroupDeleteResponseMessage * resp = [GroupDeleteResponseMessage parseFromData:resultData];
                [_delegate didDeleteGroup:resp];
                [FriendManager sharedInstance].isLatestGroup = NO;
            }
            @catch (NSException *e) {
                [[[iToast makeText:@"删除组失败!"] setDuration:2000] show];
                [_delegate didDeleteGroup:nil];
            }
        } fail:^(NSError * err) {
            [[[iToast makeText:@"网络不给力!"] setDuration:2000] show];
            [_delegate didDeleteGroup:nil];
        }];
    }
}


- (void)addFriendByPhone:(NSString*)phoneNum{
    
    PhoneNoCheckRequestMessage* msg = [[[PhoneNoCheckRequestMessage builder] addPhoneNo:phoneNum] build];
    NSData * queryData = [msg data];
    if (queryData) {
        [super noCacheRequestURL:URL_PHONE_CHECK postData:queryData completion:^(NSData * resultData) {
            @try {
                PhoneNoCheckResponseMessage * resp = [PhoneNoCheckResponseMessage parseFromData:resultData];
                [_addDelegate didAddByPhone:resp];
            
            }
            @catch (NSException *e) {
                [_addDelegate failAddByPhone];
            }
        } fail:^(NSError * err) {
            [_addDelegate failAddByPhone];
        }];
    }
}


- (void)addFriendByUid:(long long)uid myId:(long long)myId{
    
    FriendAddRequestMessage* msg = [[[[FriendAddRequestMessage builder] setUserId:myId] addFriendIdList:uid] build];
    NSData * queryData = [msg data];
    if (queryData) {
        [super noCacheRequestURL:URL_ADD_FRIEND postData:queryData completion:^(NSData * resultData) {
            @try {
                FriendAddResponseMessage * resp = [FriendAddResponseMessage parseFromData:resultData];
                [_addDelegate didAddByUid:resp];
            }
            @catch (NSException *e) {
                [_addDelegate failAddByUid];
            }
        } fail:^(NSError * err) {
            [_addDelegate failAddByUid];
        }];
    }

}

- (void)addFriendByUids:(NSArray*)uids myId:(long long)myId{
   
    FriendAddRequestMessage_Builder* builder = [FriendAddRequestMessage builder];
    [builder setUserId:myId];
    for(int i=0; i<[uids count]; i++){
        NSNumber* number = (NSNumber*)[uids objectAtIndex:i];
        long long uid = [number longLongValue];
        [builder addFriendIdList:uid];
    }
    FriendAddRequestMessage* msg = [builder build];
    
    NSData * queryData = [msg data];
    if (queryData) {
        [super noCacheRequestURL:URL_ADD_FRIEND postData:queryData completion:^(NSData * resultData) {
            @try {
                FriendAddResponseMessage * resp = [FriendAddResponseMessage parseFromData:resultData];
                [_addDelegate didAddByUid:resp];
            }
            @catch (NSException *e) {
                [_addDelegate failAddByUid];
            }
        } fail:^(NSError * err) {
            [_addDelegate failAddByUid];
        }];
    }

}


- (void)delFriendByUid:(long long)uid myId:(long long)myId{
    FriendDeleteRequestMessage_Builder * builder = [FriendDeleteRequestMessage builder];
    [builder setUserId:myId];
    [builder setFriendId:uid];
    NSData * queryData = [[builder build] data];
    
    if (queryData) {
        [super noCacheRequestURL:URL_DELETE_FRIEND postData:queryData completion:^(NSData * resultData) {
            @try {
                FriendDeleteResponseMessage * resp = [FriendDeleteResponseMessage parseFromData:resultData];
                [_friendDelDelegate didFinishDel:resp];
            }
            @catch (NSException *e) {
                [_friendDelDelegate failDel];
            }
        } fail:^(NSError * err) {
            [_friendDelDelegate failDel];
        }];
    }
}


- (void)inviteRequest:(long long)userId{

    FriendInvitationRequestMessage* msg = [[[FriendInvitationRequestMessage builder]setUserId:userId]build];
    NSData * queryData = [msg data];
    if (queryData) {
        [super noCacheRequestURL:URL_INVITE postData:queryData completion:^(NSData * resultData) {
            @try {
                FriendInvitationResponseMessage * resp = [FriendInvitationResponseMessage parseFromData:resultData];
                [_inviteDelegate didFinishInviteDelegate:resp];
            }
            @catch (NSException *e) {
                [_inviteDelegate failInvite];
            }
        } fail:^(NSError * err) {
            [_inviteDelegate failInvite];
        }];
    } else {
        [_inviteDelegate failInvite];
    }

}


- (void)handInvite:(long long)userId inviteUserId:(long long)invId state:(int)state{
    
    FriendInvitationProcessRequestMessage* msg = [[[[[FriendInvitationProcessRequestMessage builder] setUserId:userId] setInvitationUserId:invId] setStatus:state] build];
    NSData * queryData = [msg data];
    if (queryData) {
        [super noCacheRequestURL:URL_INVITE_PROCESS postData:queryData completion:^(NSData * resultData) {
            @try {
                FriendInvitationProcessResponseMessage * resp = [FriendInvitationProcessResponseMessage parseFromData:resultData];
                [_handleDelegate didFinishHandle:resp];
            }
            @catch (NSException *e) {
                [_handleDelegate failHandle];
            }
        } fail:^(NSError * err) {
            [_handleDelegate failHandle];
        }];
    } else {
        [_handleDelegate failHandle];
    }
}


- (void)phoneNumCheck:(NSArray*)arrNum{
    
    PhoneNoCheckRequestMessage_Builder* builder = [PhoneNoCheckRequestMessage builder];
    
    for(id number in arrNum){
        NSString* strNum = (NSString*)number;
        [builder addPhoneNo:strNum];
    }
    NSData* queryData = [[builder build] data];
    if (queryData) {
        [super noCacheRequestURL:URL_PHONE_CHECK postData:queryData completion:^(NSData * resultData) {
            @try {
                PhoneNoCheckResponseMessage * resp = [PhoneNoCheckResponseMessage parseFromData:resultData];
                [_phoneCheckDelegate didFinishNoCheck:resp];
            }
            @catch (NSException *e) {
                [_phoneCheckDelegate failNoCheck];
            }
        } fail:^(NSError * err) {
            [_phoneCheckDelegate failNoCheck];
        }];
    } else {
        [_phoneCheckDelegate failNoCheck];
    }
}


@end
